import request from '@/http';
import { 
  User, 
  CreateUserRequest, 
  UpdateUserRequest, 
  UserProfile,
  UpdateUserProfileRequest
} from '@/types/user';
import { ResponseData } from '@/http/types';
import { PaginatedResponse } from '@/types/common';
import { AxiosResponse } from 'axios';

const Url = {
  users: '/api/account/admin/user/',
  userProfiles: '/api/account/admin/userprofile/',
}

export default {
  // 获取用户列表
  async getUsers(page: number = 1, keyword: string = ''): Promise<AxiosResponse<ResponseData<PaginatedResponse<User>>>> {
    let url = `${Url.users}?page=${page}`;
    if (keyword) {
      url += `&keyword=${encodeURIComponent(keyword)}`;
    }
    return request.get(url);
  },

  // 获取用户详情
  async getUser(id: number): Promise<AxiosResponse<ResponseData<User>>> {
    return request.get(`${Url.users}${id}/`);
  },

  // 创建用户
  async createUser(userData: CreateUserRequest): Promise<AxiosResponse<ResponseData<User>>> {
    return request.post(Url.users, userData);
  },

  // 更新用户
  async updateUser(id: number, userData: UpdateUserRequest): Promise<AxiosResponse<ResponseData<User>>> {
    return request.patch(`${Url.users}${id}/`, userData);
  },

  // 禁用/启用用户
  async toggleUserStatus(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.post(`${Url.users}${id}/disable_user/`);
  },

  // 删除用户
  async deleteUser(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.delete(`${Url.users}${id}/`);
  },

  // 重置用户密码
  async resetUserPassword(id: number, passwords: { password: string, password_confirm: string }): Promise<AxiosResponse<ResponseData<null>>> {
    return request.post(`${Url.users}${id}/reset_password/`, passwords);
  },

  // 删除用户的个人资料
  async deleteUserProfile(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.delete(`${Url.users}${id}/delete_profile/`);
  },

  // 创建用户的个人资料
  async createUserProfile(id: number): Promise<AxiosResponse<ResponseData<UserProfile>>> {
    return request.post(`${Url.users}${id}/create_profile/`);
  },

  // 获取用户配置文件列表
  async getUserProfiles(page: number = 1): Promise<AxiosResponse<ResponseData<PaginatedResponse<UserProfile>>>> {
    return request.get(`${Url.userProfiles}?page=${page}`);
  },

  // 获取用户配置文件详情
  async getUserProfile(id: number): Promise<AxiosResponse<ResponseData<UserProfile>>> {
    return request.get(`${Url.userProfiles}${id}/`);
  },

  // 更新用户配置文件
  async updateUserProfile(id: number, profileData: UpdateUserProfileRequest): Promise<AxiosResponse<ResponseData<UserProfile>>> {
    return request.patch(`${Url.userProfiles}${id}/`, profileData);
  },

  // 获取头像完整URL
  getAvatarUrl(path: string): string {
    return request.getAvatarUrl(path);
  }
}; 